home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / compiler / ctype.lisp < prev    next >
Encoding:
Text File  |  1991-12-12  |  27.0 KB  |  783 lines

  1. ;;; -*- Package: C; Log: C.Log -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: ctype.lisp,v 1.25 91/12/11 23:49:53 ram Exp $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;;    This file contains code which knows about both the type representation
  15. ;;; and the compiler IR1 representation.  This stuff is used for doing type
  16. ;;; checking.
  17. ;;;
  18. ;;; Written by Rob MacLachlan
  19. ;;;
  20. (in-package 'c)
  21.  
  22. ;;; These are the functions that are to be called when a problem is detected.
  23. ;;; They are passed format arguments.  If null, we don't do anything.  The
  24. ;;; error function is called when something is definitely incorrect.  The
  25. ;;; warning function is called when it is somehow impossible to tell if the
  26. ;;; call is correct.
  27. ;;;
  28. (defvar *error-function*)
  29. (defvar *warning-function*)
  30.  
  31. ;;; The function that we use for type checking.  The derived type is the first
  32. ;;; argument and the type we are testing against is the second argument.  The
  33. ;;; function should return values like Csubtypep. 
  34. ;;;
  35. (defvar *test-function*)
  36.  
  37. (proclaim '(type (or function null) *error-function* *warning-function
  38.          *test-function*))
  39.  
  40. ;;; *lossage-detected* is set if a definite incompatibility is detected.
  41. ;;; *slime-detected* is set if we can't tell whether the call is compatible or
  42. ;;; not.
  43. ;;;
  44. (defvar *lossage-detected*)
  45. (defvar *slime-detected*)
  46.  
  47.  
  48. ;;; Note-Lossage, Note-Slime  --  Internal
  49. ;;;
  50. ;;;    Signal a warning if appropriate and set the *lossage-detected* flag.
  51. ;;;
  52. (proclaim '(ftype (function (string &rest t) void) note-lossage note-slime))
  53. (defun note-lossage (format-string &rest format-args)
  54.   (setq *lossage-detected* t)
  55.   (when *error-function*
  56.     (apply *error-function* format-string format-args)))
  57. ;;;
  58. (defun note-slime (format-string &rest format-args)
  59.   (setq *slime-detected* t)
  60.   (when *warning-function*
  61.     (apply *warning-function* format-string format-args)))
  62.  
  63.  
  64. (proclaim '(special *compiler-error-context*))
  65.  
  66.  
  67. ;;;; Stuff for checking a call against a function type.
  68.  
  69. ;;; ALWAYS-SUBTYPEP  --  Interface
  70. ;;;
  71. ;;;    A dummy version of SUBTYPEP useful when we want a functional like
  72. ;;; subtypep that always returns true.
  73. ;;;
  74. (defun always-subtypep (type1 type2)
  75.   (declare (ignore type1 type2))
  76.   (values t t))
  77.  
  78.  
  79. ;;; Valid-Function-Use  --  Interface
  80. ;;;
  81. ;;;    Determine whether a use of a function is consistent with its type.
  82. ;;; These values are returned:
  83. ;;;    T, T: the call is definitely valid.
  84. ;;;    NIL, T: the call is definitely invalid.
  85. ;;;    NIL, NIL: unable to determine if the call is valid.
  86. ;;;
  87. ;;; The Argument-Test function is used to determine whether an argument type
  88. ;;; matches the type we are checking against.  Similarly, the Result-Test is
  89. ;;; used to determine whether the result type matches the specified result.
  90. ;;;
  91. ;;; Unlike the argument test, the result test may be called on values or
  92. ;;; function types.  If Strict-Result is true and safety is non-zero, then the
  93. ;;; Node-Derived-Type is always used.  Otherwise, if Cont's Type-Check is true,
  94. ;;; then the Node-Derived-Type is intersected with the Cont's Asserted-Type.
  95. ;;;
  96. ;;; The error and warning functions are functions that are called to explain
  97. ;;; the result.  We bind *compiler-error-context* to the combination node so
  98. ;;; that Compiler-Warning and related functions will do the right thing if
  99. ;;; they are supplied.
  100. ;;;
  101. (defun valid-function-use (call type &key
  102.                 ((:argument-test *test-function*) #'csubtypep)
  103.                 (result-test #'values-subtypep)
  104.                 (strict-result nil)
  105.                 ((:error-function *error-function*))
  106.                 ((:warning-function *warning-function*)))
  107.   (declare (type function result-test) (type combination call)
  108.        (type function-type type))
  109.   (let* ((*lossage-detected* nil)
  110.      (*slime-detected* nil)
  111.      (*compiler-error-context* call)
  112.      (args (combination-args call))
  113.      (nargs (length args))
  114.      (required (function-type-required type))
  115.      (min-args (length required))
  116.      (optional (function-type-optional type))
  117.      (max-args (+ min-args (length optional)))
  118.      (rest (function-type-rest type))
  119.      (keyp (function-type-keyp type)))
  120.     
  121.     (cond
  122.      ((function-type-wild-args type)
  123.       (do ((i 1 (1+ i))
  124.        (arg args (cdr arg)))
  125.       ((null arg))
  126.     (check-arg-type (car arg) *wild-type* i)))
  127.      ((not (or optional keyp rest))
  128.       (if (/= nargs min-args)
  129.       (note-lossage
  130.        "Function called with ~R argument~:P, but wants exactly ~R."
  131.        nargs min-args)
  132.       (check-fixed-and-rest args required nil)))
  133.      ((< nargs min-args)
  134.       (note-lossage
  135.        "Function called with ~R argument~:P, but wants at least ~R."
  136.        nargs min-args))
  137.      ((<= nargs max-args)
  138.       (check-fixed-and-rest args (append required optional) rest))
  139.      ((not (or keyp rest))
  140.       (note-lossage
  141.        "Function called with ~R argument~:P, but wants at most ~R."
  142.        nargs max-args))
  143.      ((and keyp (oddp (- nargs max-args)))
  144.       (note-lossage
  145.        "Function has an odd number of arguments in the keyword portion."))
  146.      (t
  147.       (check-fixed-and-rest args (append required optional) rest)
  148.       (when keyp
  149.     (check-keywords args max-args type))))
  150.     
  151.     (let* ((dtype (node-derived-type call))
  152.        (return-type (function-type-returns type))
  153.        (cont (node-cont call))
  154.        (out-type
  155.         (if (or (not (continuation-type-check cont))
  156.             (and strict-result (policy call (/= safety 0))))
  157.         dtype
  158.         (values-type-intersection (continuation-asserted-type cont)
  159.                       dtype))))
  160.       (multiple-value-bind (int win)
  161.                (funcall result-test out-type return-type)
  162.     (cond ((not win)
  163.            (note-slime "Can't tell whether the result is a ~S."
  164.                (type-specifier return-type)))
  165.           ((not int)
  166.            (note-lossage "The result is a ~S, not a ~S."
  167.                  (type-specifier out-type)
  168.                  (type-specifier return-type)))))) 
  169.     
  170.     (cond (*lossage-detected* (values nil t))
  171.       (*slime-detected* (values nil nil))
  172.       (t (values t t)))))
  173.  
  174.  
  175. ;;; Check-Arg-Type  --  Internal
  176. ;;;
  177. ;;;    Check that the derived type of the continuation Cont is compatible with
  178. ;;; Type.  N is the arg number, for error message purposes.  We return true if
  179. ;;; arg is definitely o.k.  If the type is a magic CONSTANT-TYPE, then we check
  180. ;;; for the argument being a constant value of the specified type.  If there is
  181. ;;; a manfest type error (DERIVED-TYPE = NIL), then we flame about the asserted
  182. ;;; type even when our type is satisfied under the test.
  183. ;;;
  184. (defun check-arg-type (cont type n)
  185.   (declare (type continuation cont) (type ctype type) (type index n))
  186.   (cond
  187.    ((not (constant-type-p type))
  188.     (let ((ctype (continuation-type cont)))
  189.       (multiple-value-bind (int win)
  190.                (funcall *test-function* ctype type)
  191.     (cond ((not win)
  192.            (note-slime "Can't tell whether the ~:R argument is a ~S." n
  193.                (type-specifier type))
  194.            nil)
  195.           ((not int)
  196.            (note-lossage "The ~:R argument is a ~S, not a ~S." n
  197.                  (type-specifier ctype)
  198.                  (type-specifier type))
  199.            nil)
  200.           ((eq ctype *empty-type*)
  201.            (note-slime "The ~:R argument never returns a value." n)
  202.            nil)
  203.           (t t)))))
  204.     ((not (constant-continuation-p cont))
  205.      (note-slime "The ~:R argument is not a constant." n)
  206.      nil)
  207.     (t
  208.      (let ((val (continuation-value cont))
  209.        (type (constant-type-type type)))
  210.        (multiple-value-bind (res win)
  211.                 (ctypep val type)
  212.      (cond ((not win)
  213.         (note-slime "Can't tell whether the ~:R argument is a ~
  214.                      constant ~S:~%  ~S"
  215.                 n (type-specifier type) val)
  216.         nil)
  217.            ((not res)
  218.         (note-lossage "The ~:R argument is not a constant ~S:~%  ~S"
  219.                   n (type-specifier type) val)
  220.         nil)
  221.            (t t)))))))
  222.  
  223.   
  224. ;;; Check-Fixed-And-Rest  --  Internal
  225. ;;;
  226. ;;;    Check that each of the type of each supplied argument intersects with
  227. ;;; the type specified for that argument.  If we can't tell, then we complain
  228. ;;; about the slime.
  229. ;;;
  230. (proclaim '(function check-fixed-and-rest (list list (or ctype null)) void))
  231. (defun check-fixed-and-rest (args types rest)
  232.   (do ((arg args (cdr arg))
  233.        (type types (cdr type))
  234.        (n 1 (1+ n)))
  235.       ((or (null type) (null arg))
  236.        (when rest
  237.      (dolist (arg arg)
  238.        (check-arg-type arg rest n)
  239.        (incf n))))
  240.     (declare (fixnum n))
  241.     (check-arg-type (car arg) (car type) n)))
  242.  
  243.  
  244. ;;; Check-Keywords  --  Internal
  245. ;;;
  246. ;;;    Check that the keyword args are of the correct type.  Each keyword
  247. ;;; should be known and the corresponding argument should be of the correct
  248. ;;; type.  If the keyword isn't a constant, then we can't tell, so we note
  249. ;;; slime.
  250. ;;;
  251. (proclaim '(function check-keywords (list fixnum function-type) void))
  252. (defun check-keywords (args pre-key type)
  253.   (do ((key (nthcdr pre-key args) (cddr key))
  254.        (n (1+ pre-key) (+ n 2)))
  255.       ((null key))
  256.     (declare (fixnum n))
  257.     (let ((k (car key)))
  258.       (cond
  259.        ((not (check-arg-type k (specifier-type 'symbol) n)))
  260.        ((not (constant-continuation-p k))
  261.     (note-slime "The ~:R argument (in keyword position) is not a constant."
  262.             n))
  263.        (t
  264.     (let* ((name (continuation-value k))
  265.            (info (find name (function-type-keywords type)
  266.                :key #'key-info-name)))
  267.       (cond ((not info)
  268.          (unless (function-type-allowp type)
  269.            (note-lossage "~S is not a known argument keyword."
  270.                  name)))
  271.         (t
  272.          (check-arg-type (second key) (key-info-type info)
  273.                  (1+ n))))))))))
  274.  
  275.  
  276. ;;; Definition-Type  --  Interface
  277. ;;;
  278. ;;;    Construct a function type from a definition.
  279. ;;;
  280. ;;; Due to the lack of a (list x) type specifier, we can't reconstruct the
  281. ;;; &rest type.
  282. ;;;
  283. (proclaim '(function definition-type (functional) function-type))
  284. (defun definition-type (functional)
  285.   (if (lambda-p functional)
  286.       (make-function-type
  287.        :required (mapcar #'leaf-type (lambda-vars functional))
  288.        :returns (tail-set-type (lambda-tail-set functional)))
  289.       (let ((rest nil))
  290.     (collect ((req)
  291.           (opt)
  292.           (keys))
  293.       (dolist (arg (optional-dispatch-arglist functional))
  294.         (let ((info (lambda-var-arg-info arg))
  295.           (type (leaf-type arg)))
  296.           (if info
  297.           (ecase (arg-info-kind info)
  298.             (:required (req type))
  299.             (:optional (opt type))
  300.             (:keyword
  301.              (keys (make-key-info :name (arg-info-keyword info)
  302.                       :type type)))
  303.             (:rest
  304.              (setq rest *universal-type*)))
  305.           (req type))))
  306.       
  307.       (make-function-type
  308.        :required (req)  :optional (opt)  :rest rest  :keywords (keys)
  309.        :keyp (optional-dispatch-keyp functional)
  310.        :allowp (optional-dispatch-allowp functional)
  311.        :returns (tail-set-type
  312.              (lambda-tail-set
  313.               (optional-dispatch-main-entry functional))))))))
  314.  
  315.  
  316.  
  317. ;;;; Approximate function types:
  318. ;;;
  319. ;;;    Approximate function types provide a condensed representation of all the
  320. ;;; different ways that a function has been used.  If we have no declared or
  321. ;;; defined type for a function, then we build an approximate function type
  322. ;;; by examining each use of the function.  When we encounter a definition or
  323. ;;; proclamation, we can check the actual type for compatibity with the
  324. ;;; previous uses.
  325.  
  326.  
  327. (defstruct (approximate-function-type)
  328.   ;;
  329.   ;; The smallest and largest numbers of arguments that this function has been
  330.   ;; called with.
  331.   (min-args call-arguments-limit :type fixnum)
  332.   (max-args 0 :type fixnum)
  333.   ;;
  334.   ;; A list of lists of the all the types that have been used in each argument
  335.   ;; position.
  336.   (types () :type list)
  337.   ;;
  338.   ;; A list of the Approximate-Key-Info structures describing all the things
  339.   ;; that looked like keyword arguments.  There are distinct structures
  340.   ;; describing each argument position in which the keyword appeared.
  341.   (keys () :type list))
  342.  
  343.  
  344. (defstruct (approximate-key-info)
  345.   ;;
  346.   ;; The keyword name of this argument.  Although keyword names don't have to
  347.   ;; be keywords, we only match on keywords when figuring an approximate type.
  348.   (name (required-argument) :type keyword)
  349.   ;;
  350.   ;; The position at which this keyword appeared.  0 if it appeared as the
  351.   ;; first argument, etc.
  352.   (position (required-argument) :type fixnum)
  353.   ;;
  354.   ;; A list of all the argument types that have been used with this keyword.
  355.   (types nil :type list)
  356.   ;;
  357.   ;; True if this keyword has appeared only in calls with an obvious
  358.   ;; :allow-other-keys.
  359.   (allowp nil :type (member t nil)))
  360.  
  361.  
  362. ;;; Note-Function-Use  --  Interface
  363. ;;;
  364. ;;;    Return an Approximate-Function-Type representing the context of Call.
  365. ;;; If Type is supplied and not null, then we merge the information into the
  366. ;;; information already accumulated in Type.
  367. ;;;
  368. (proclaim '(function note-function-use
  369.              (combination &optional (or approximate-function-type null))
  370.              approximate-function-type))
  371. (defun note-function-use (call &optional type)
  372.   (let* ((type (or type (make-approximate-function-type)))
  373.      (types (approximate-function-type-types type))
  374.      (args (combination-args call))
  375.      (nargs (length args))
  376.      (allowp (some #'(lambda (x)
  377.                (and (constant-continuation-p x)
  378.                 (eq (continuation-value x) :allow-other-keys)))
  379.               args)))
  380.  
  381.     (setf (approximate-function-type-min-args type)
  382.       (min (approximate-function-type-min-args type) nargs))
  383.     (setf (approximate-function-type-max-args type)
  384.       (max (approximate-function-type-max-args type) nargs))
  385.  
  386.     (do ((old types (cdr old))
  387.      (arg args (cdr arg)))
  388.     ((null old)
  389.      (setf (approximate-function-type-types type)
  390.            (nconc types
  391.               (mapcar #'(lambda (x)
  392.                   (list (continuation-type x)))
  393.                   arg))))
  394.       (when (null arg) (return))
  395.       (pushnew (continuation-type (car arg))
  396.            (car old)
  397.            :test #'type=))
  398.  
  399.     (collect ((keys (approximate-function-type-keys type) cons))
  400.       (do ((arg args (cdr arg))
  401.        (pos 0 (1+ pos)))
  402.       ((or (null arg) (null (cdr arg)))
  403.        (setf (approximate-function-type-keys type) (keys)))
  404.     (let ((key (first arg))
  405.           (val (second arg)))
  406.       (when (constant-continuation-p key)
  407.         (let ((name (continuation-value key)))
  408.           (when (keywordp name)
  409.         (let ((old (find-if
  410.                 #'(lambda (x)
  411.                 (and (eq (approximate-key-info-name x) name)
  412.                      (= (approximate-key-info-position x)
  413.                     pos)))
  414.                 (keys)))
  415.               (val-type (continuation-type val))) 
  416.           (cond (old
  417.              (pushnew val-type
  418.                   (approximate-key-info-types old)
  419.                   :test #'type=)
  420.              (unless allowp
  421.                (setf (approximate-key-info-allowp old) nil)))
  422.             (t
  423.              (keys (make-approximate-key-info
  424.                 :name name  :position pos  :allowp allowp
  425.                 :types (list val-type))))))))))))
  426.     type))
  427.  
  428.  
  429. ;;; Valid-Approximate-Type  --  Interface
  430. ;;;
  431. ;;;    Similar to Valid-Function-Use, but checks an Approximate-Function-Type
  432. ;;; against a real function type.
  433. ;;;
  434. (proclaim '(function valid-approximate-type
  435.              (approximate-function-type function-type &optional
  436.                         function function function)
  437.              (values boolean boolean)))
  438. (defun valid-approximate-type (call-type type &optional
  439.                      (*test-function* #'types-intersect)
  440.                      (*error-function* #'compiler-warning)
  441.                      (*warning-function* #'compiler-note))
  442.   (let* ((*lossage-detected* nil)
  443.      (*slime-detected* nil)
  444.      (required (function-type-required type))
  445.      (min-args (length required))
  446.      (optional (function-type-optional type))
  447.      (max-args (+ min-args (length optional)))
  448.      (rest (function-type-rest type))
  449.      (keyp (function-type-keyp type)))
  450.  
  451.     (when (function-type-wild-args type)
  452.       (return-from valid-approximate-type (values t t)))
  453.  
  454.     (let ((call-min (approximate-function-type-min-args call-type)))
  455.       (when (< call-min min-args)
  456.     (note-lossage
  457.      "Function previously called with ~R argument~:P, but wants at least ~R."
  458.      call-min min-args)))
  459.  
  460.     (let ((call-max (approximate-function-type-max-args call-type)))
  461.       (cond ((<= call-max max-args))
  462.         ((not (or keyp rest))
  463.          (note-lossage
  464.           "Function previously called with ~R argument~:P, but wants at most ~R."
  465.           call-max max-args))
  466.         ((and keyp (oddp (- call-max max-args)))
  467.          (note-lossage
  468.           "Function previously called with an odd number of arguments in ~
  469.           the keyword portion.")))
  470.  
  471.       (when (and keyp (> call-max max-args))
  472.     (check-approximate-keywords call-type max-args type)))
  473.  
  474.     (check-approximate-fixed-and-rest call-type (append required optional)
  475.                       rest)
  476.  
  477.     (cond (*lossage-detected* (values nil t))
  478.       (*slime-detected* (values nil nil))
  479.       (t (values t t)))))
  480.  
  481.  
  482. ;;; Check-Approximate-Fixed-And-Rest  --  Internal
  483. ;;;
  484. ;;;    Check that each of the types used at each arg position is compatible
  485. ;;; with the actual type.
  486. ;;;
  487. (proclaim '(function check-approximate-fixed-and-rest
  488.              (approximate-function-type list (or ctype null))
  489.              void))
  490. (defun check-approximate-fixed-and-rest (call-type fixed rest)
  491.   (do ((types (approximate-function-type-types call-type) (cdr types))
  492.        (n 1 (1+ n))
  493.        (arg fixed (cdr arg)))
  494.       ((null types))
  495.     (let ((decl-type (or (car arg) rest)))
  496.       (unless decl-type (return))
  497.       (check-approximate-arg-type (car types) decl-type "~:R" n))))
  498.  
  499.  
  500. ;;; Check-Approximate-Arg-Type  --  Internal
  501. ;;;
  502. ;;;    Check that each of the call-types is compatible with Decl-Type,
  503. ;;; complaining if not or if we can't tell.
  504. ;;;
  505. (proclaim '(function check-approximate-arg-type
  506.              (list ctype string &rest t)
  507.              void))
  508. (defun check-approximate-arg-type (call-types decl-type context &rest args)
  509.   (let ((losers *empty-type*))
  510.     (dolist (ctype call-types)
  511.       (multiple-value-bind (int win)
  512.                (funcall *test-function* ctype decl-type)
  513.     (cond
  514.      ((not win)
  515.       (note-slime "Can't tell whether previous ~? argument type ~S is a ~S."
  516.               context args (type-specifier ctype) (type-specifier decl-type)))
  517.      ((not int)
  518.       (setq losers (type-union ctype losers))))))
  519.  
  520.     (unless (eq losers *empty-type*)
  521.       (note-lossage "~:(~?~) argument should be a ~S but was a ~S in a previous call."
  522.             context args (type-specifier decl-type) (type-specifier losers)))))
  523.  
  524.  
  525. ;;; Check-Approximate-Keywords  --  Internal
  526. ;;;
  527. ;;;    Check the types of each manifest keyword that appears in a keyword
  528. ;;; argument position.  Check the validity of all keys that appeared in valid
  529. ;;; keyword positions.
  530. ;;;
  531. ;;; ### We could check the Approximate-Function-Type-Types to make sure that
  532. ;;; all arguments in keyword positions were manifest keywords.
  533. ;;;
  534. (defun check-approximate-keywords (call-type max-args type)
  535.   (let ((call-keys (approximate-function-type-keys call-type))
  536.     (keys (function-type-keywords type)))
  537.     (dolist (key keys)
  538.       (let ((name (key-info-name key)))
  539.     (collect ((types nil append))
  540.       (dolist (call-key call-keys)
  541.         (let ((pos (approximate-key-info-position call-key)))
  542.           (when (and (eq (approximate-key-info-name call-key) name)
  543.              (> pos max-args) (evenp (- pos max-args)))
  544.         (types (approximate-key-info-types call-key)))))
  545.       (check-approximate-arg-type (types) (key-info-type key) "~S" name))))
  546.     
  547.     (unless (function-type-allowp type)
  548.       (collect ((names () adjoin))
  549.     (dolist (call-key call-keys)
  550.       (let ((pos (approximate-key-info-position call-key)))
  551.         (when (and (> pos max-args) (evenp (- pos max-args))
  552.                (not (approximate-key-info-allowp call-key)))
  553.           (names (approximate-key-info-name call-key)))))
  554.  
  555.     (dolist (name (names))
  556.       (unless (find name keys :key #'key-info-name)
  557.         (note-lossage "Function previously called with unknown argument keyword ~S."
  558.           name)))))))
  559.  
  560.  
  561. ;;;; ASSERT-DEFINITION-TYPE
  562.  
  563. ;;; TRY-TYPE-INTERSECTIONS  --  Internal
  564. ;;;
  565. ;;;    Intersect Lambda's var types with Types, giving a warning if there is a
  566. ;;; mismatch.  If all intersections are non-null, we return lists of the
  567. ;;; variables and intersections, otherwise we return NIL, NIL.
  568. ;;;
  569. (defun try-type-intersections (vars types where)
  570.   (declare (list vars types) (string where))
  571.   (collect ((res))
  572.     (mapc #'(lambda (var type)
  573.           (let* ((vtype (leaf-type var))
  574.              (int (type-intersection vtype type)))
  575.         (cond
  576.          ((eq int *empty-type*)
  577.           (note-lossage
  578.            "Definition's declared type for variable ~A:~%  ~S~@
  579.            conflicts with this type from ~A:~%  ~S"
  580.            (leaf-name var) (type-specifier vtype)
  581.            where (type-specifier type))
  582.           (return-from try-type-intersections (values nil nil)))
  583.          (t
  584.           (res int)))))
  585.       vars types)
  586.     (values vars (res))))
  587.  
  588.  
  589. ;;; FIND-OPTIONAL-DISPATCH-TYPES  --  Internal
  590. ;;;
  591. ;;;    Check that the optional-dispatch OD conforms to Type.  We return the
  592. ;;; values of TRY-TYPE-INTERSECTIONS if there are no syntax problems, otherwise
  593. ;;; NIL, NIL.
  594. ;;;
  595. ;;;    Note that the variables in the returned list are the actual original
  596. ;;; variables (extracted from the optional dispatch arglist), rather than the
  597. ;;; variables that are arguments to the main entry.  This difference is
  598. ;;; significant only for keyword args with hairy defaults.  Returning the
  599. ;;; actual vars allows us to use the right variable name in warnings.
  600. ;;;
  601. ;;;    A slightly subtle point: with keywords and optionals, the type in the
  602. ;;; function type is only an assertion on calls --- it doesn't constrain the
  603. ;;; type of default values.  So we have to union in the type of the default.
  604. ;;; With optionals, we can't do any assertion unless the default is constant.
  605. ;;;
  606. ;;;    With keywords, we exploit our knowledge about how hairy keyword
  607. ;;; defaulting is done when computing the type assertion to put on the
  608. ;;; main-entry argument.  In the case of hairy keywords, the default has been
  609. ;;; clobbered with NIL, which is the value of the main-entry arg in the
  610. ;;; unsupplied case, whatever the actual default value is.  So we can just
  611. ;;; assume the default is constant, effectively unioning in NULL, and not
  612. ;;; totally blow off doing any type assertion.
  613. ;;;
  614. (defun find-optional-dispatch-types (od type where)
  615.   (declare (type optional-dispatch od) (type function-type type)
  616.        (string where))
  617.   (let* ((min (optional-dispatch-min-args od))
  618.      (req (function-type-required type))
  619.      (opt (function-type-optional type)))
  620.     (flet ((frob (x y what)
  621.          (unless (= x y)
  622.            (note-lossage
  623.         "Definition has ~R ~A arg~P, but ~A has ~R."
  624.         x what x where y))))
  625.       (frob min (length req) "fixed")
  626.       (frob (- (optional-dispatch-max-args od) min) (length opt) "optional"))
  627.     (flet ((frob (x y what)
  628.          (unless (eq x y)
  629.            (note-lossage
  630.         "Definition ~:[doesn't have~;has~] ~A, but ~
  631.         ~A ~:[doesn't~;does~]."
  632.         x what where y))))
  633.       (frob (optional-dispatch-keyp od) (function-type-keyp type)
  634.         "keyword args")
  635.       (unless (optional-dispatch-keyp od)
  636.     (frob (not (null (optional-dispatch-more-entry od)))
  637.           (not (null (function-type-rest type)))
  638.           "rest args"))
  639.       (frob (optional-dispatch-allowp od) (function-type-allowp type)
  640.         "&allow-other-keys"))
  641.  
  642.     (when *lossage-detected*
  643.       (return-from find-optional-dispatch-types (values nil nil)))
  644.  
  645.     (collect ((res)
  646.           (vars))
  647.       (let ((keys (function-type-keywords type))
  648.         (arglist (optional-dispatch-arglist od)))
  649.     (dolist (arg arglist)
  650.       (cond
  651.        ((lambda-var-arg-info arg)
  652.         (let* ((info (lambda-var-arg-info arg))
  653.            (default (arg-info-default info))
  654.            (def-type (when (constantp default)
  655.                    (ctype-of (eval default)))))
  656.           (ecase (arg-info-kind info)
  657.         (:keyword
  658.          (let* ((key (arg-info-keyword info))
  659.             (kinfo (find key keys :key #'key-info-name)))
  660.            (cond
  661.             (kinfo
  662.              (res (type-union (key-info-type kinfo) def-type)))
  663.             (t
  664.              (note-lossage
  665.               "Defining a ~S keyword not present in ~A."
  666.               key where)
  667.              (res *universal-type*)))))
  668.         (:required (res (pop req)))
  669.         (:optional
  670.          (res (type-union (pop opt)
  671.                   (or def-type *universal-type*))))
  672.         (:rest
  673.          (when (function-type-rest type)
  674.            (res (specifier-type 'list)))))
  675.           (vars arg)
  676.           (when (arg-info-supplied-p info)
  677.         (res *universal-type*)
  678.         (vars (arg-info-supplied-p info)))))
  679.        (t
  680.         (res (pop req))
  681.         (vars arg))))
  682.  
  683.     (dolist (key keys)
  684.       (unless (find (key-info-name key) arglist
  685.             :key #'(lambda (x)
  686.                  (let ((info (lambda-var-arg-info x)))
  687.                    (when info
  688.                      (arg-info-keyword info)))))
  689.         (note-lossage
  690.          "Definition lacks the ~S keyword present in ~A."
  691.          (key-info-name key) where))))
  692.  
  693.       (try-type-intersections (vars) (res) where))))
  694.  
  695.  
  696. ;;; FIND-LAMBDA-TYPES  --  Internal
  697. ;;;
  698. ;;;    Check that Type doesn't specify any funny args, and do the intersection.
  699. ;;; 
  700. (defun find-lambda-types (lambda type where)
  701.   (declare (type clambda lambda) (type function-type type) (string where))
  702.   (flet ((frob (x what)
  703.        (when x
  704.          (note-lossage
  705.           "Definition has no ~A, but the ~A did."
  706.           what where))))
  707.     (frob (function-type-optional type) "optional args")
  708.     (frob (function-type-keyp type) "keyword args")
  709.     (frob (function-type-rest type) "rest arg"))
  710.   (let* ((vars (lambda-vars lambda))
  711.      (nvars (length vars))
  712.      (req (function-type-required type))
  713.      (nreq (length req)))
  714.     (unless (= nvars nreq)
  715.       (note-lossage "Definition has ~R arg~:P, but the ~A has ~R."
  716.             nvars where nreq))
  717.     (if *lossage-detected*
  718.     (values nil nil)
  719.     (try-type-intersections vars req where))))
  720.  
  721.  
  722. ;;; ASSERT-DEFINITION-TYPE  --  Interface
  723. ;;;
  724. ;;;    Check for syntactic and type conformance between the definition
  725. ;;; Functional and the specified Function-Type.  If they are compatible and
  726. ;;; Really-Assert is T, then add type assertions to the defintion from the
  727. ;;; Function-Type.
  728. ;;;
  729. ;;;    If there is a syntactic or type problem, then we call Error-Function
  730. ;;; with an error message using Where as context describing where Function-Type
  731. ;;; came from.
  732. ;;;
  733. ;;;    If there is no problem, we return T (even if Really-Assert was false).
  734. ;;; If there was a problem, we return NIL.
  735. ;;;
  736. (defun assert-definition-type
  737.        (functional type &key (really-assert t)
  738.            ((:error-function *error-function*) #'compiler-warning)
  739.            warning-function
  740.            (where "previous declaration"))
  741.   (declare (type functional functional) (type function *error-function*)
  742.        (string where))
  743.   (let ((*lossage-detected* nil))
  744.     (multiple-value-bind
  745.     (vars types)
  746.     (if (function-type-wild-args type)
  747.         (values nil nil)
  748.         (etypecase functional
  749.           (optional-dispatch
  750.            (find-optional-dispatch-types functional type where))
  751.           (clambda
  752.            (find-lambda-types functional type where))))
  753.       (let* ((type-returns (function-type-returns type))
  754.          (return (lambda-return (main-entry functional)))
  755.          (atype (when return
  756.               (continuation-asserted-type (return-result return)))))
  757.     (cond
  758.      ((and atype (not (values-types-intersect atype type-returns)))
  759.       (note-lossage
  760.        "The result type from ~A:~%  ~S~@
  761.        conflicts with the definition's result type assertion:~%  ~S"
  762.        where (type-specifier type-returns) (type-specifier atype))
  763.       nil)
  764.      (*lossage-detected* nil)
  765.      ((not really-assert) t)
  766.      (t
  767.       (when atype
  768.         (assert-continuation-type (return-result return) atype))
  769.       (loop for var in vars and type in types do
  770.         (cond ((basic-var-sets var)
  771.            (when (and warning-function
  772.                   (not (csubtypep (leaf-type var) type)))
  773.              (funcall warning-function
  774.                   "Assignment to argument: ~S~%  ~
  775.                    prevents use of assertion from function ~
  776.                    type ~A:~%  ~S~%"
  777.                   (leaf-name var) where (type-specifier type))))
  778.           (t
  779.            (setf (leaf-type var) type)
  780.            (dolist (ref (leaf-refs var))
  781.              (derive-node-type ref type)))))
  782.       t))))))
  783.